<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@include file="../../../taglib.jsp" %>
<html>
<head>
    <link rel="stylesheet" href="${path}/layui/css/layui.css">
</head>
<body>
<form class="layui-form layui-form-pane1" action="" value="2">

    <div class="layui-form-item" style="margin-top: 10px;">
        <label class="layui-form-label">角色名称</label>
        <div class="layui-input-inline">
            <select name="roleName" id="roleName" lay-filter="roleName" onchange="changeRole();">
                <option disabled>请选择</option>
            </select>
        </div>
    </div>

    <table class="layui-table">
        <thead>
        <tr>
            <th>一级菜单</th>
            <th>二级菜单</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach var="item" items="${lists}">
            <tr>
                <c:set var="superContains" value="false" />
                <c:forEach items="${rolePermissionstr}" var="item4">
                    <c:if test="${item4==item.id}">
                        <c:set var="superContains" value="true" />
                    </c:if>
                </c:forEach>
                <c:choose>
                    <c:when test="${superContains==true}">
                        <td><input name="like[write]" id="a${item.id}" value="a${item.id}" data_flag="1" title="${item.permissionName}" type="checkbox" checked></td>
                    </c:when>
                    <c:otherwise>
                        <td><input name="like[write]" id="a${item.id}" value="a${item.id}" data_flag="1" title="${item.permissionName}" type="checkbox"></td>
                    </c:otherwise>
                </c:choose>
                <c:if test="${item.childList != null}">
                    <td colspan="2" >
                        <table class="layui-table">
                            <c:forEach items="${item.childList}" var="item2">
                                <tr>
                                    <c:set var="contains" value="false" />
                                    <c:forEach items="${rolePermissionstr}" var="item3">
                                        <c:if test="${item3==item2.id}">
                                            <c:set var="contains" value="true" />
                                        </c:if>
                                    </c:forEach>
                                    <c:choose>
                                        <c:when test="${contains==true}">
                                            <td><input name="like[write]" id="a${item2.id}" value="a${item2.id}" data_flag="2" title="${item2.permissionName}" type="checkbox" checked></td>
                                        </c:when>
                                        <c:otherwise>
                                            <td><input name="like[write]" id="a${item2.id}" value="a${item2.id}" data_flag="2" title="${item2.permissionName}" type="checkbox"></td>
                                        </c:otherwise>
                                    </c:choose>
                                </tr>
                            </c:forEach>
                        </table>
                    </td>
                </c:if>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</form>
<script>
    var userId = '${userId}';
    var $;
    layui.use('form', function(){
        var form = layui.form();
        $ = layui.jquery;

        $(document).ready(function () {
            $.ajax({
                url : '${path}/user/initRoleName.do',
                data :{
                    userId: userId,
                    roleId : '${roleId}'
                },
                type:'post',
                success:function(data){
                    $("#roleName").append(data);
                    form.render('select');
                },
                error : function() {
                    layer.alert("登录异常！");
                }
            })
        });

        form.on('select(roleName)', function(data){
            var roleId = data.value;
            window.location.href = '${path}/user/showEditRolePage.do?userId='+userId+"&roleId="+roleId;
        });

        form.on('checkbox',function (data) {
            var id = data.value;
            var checkFlag = data.elem.checked;      //获取的是点击后的选中状态
            if(checkFlag==true){
                $("#"+id).attr("checked",false);
                $("#"+id).siblings().removeClass('layui-form-checked');
                return false;
            }else{
                $("#"+id).attr("checked",true);
                $("#"+id).siblings().addClass('layui-form-checked');
                return false;
            }
        })

    });
    function submitIframe() {
        var roleId = $("#roleName").select().val();
        var obj = new Object();
        obj.userId = userId;
        obj.roleId = roleId;
        return obj;
    }
</script>
</body>
</html>
